Reverse linked list
corner case
1
2dummy->1->null
dummy->nullgeneral case
1
2
3
4
5
6
7
8
9
10
11dummy-> 1 -> 2 -> 3 -> null
pre cur nextNode
dummy -> 2 -> 1 -> 3 -> null
pre nextNode cur
dummy -> 2 -> 1 -> 3 -> null
pre cur nextNode
dummy -> 3 -> 2 -> 1 -> null
pre cur nextNode
- link cur to the next node
cur.next = nextNode.next
- link nextNode to the head of the reversed part
nextNode.next = pre.next
- link pre to the nextNode
pre.next = nextNode
- update nextNode to next
nextNode = cur.next
- when nextNode gets to the end, exit
cur represents the tail, and pre represents the the dummy, nextNode represents the node waiting to be added to the head of the list.